package com.wwwarehouse.usercenter.fragment.business_relationship;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.adapter.BRCFDTagsUpdateAdapter;
import com.wwwarehouse.usercenter.bean.business_relationship.BRCFDTagsBean;
import com.wwwarehouse.usercenter.bean.business_relationship.BRCustomerFileDetailBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


/**
 * @author : mundane
 * @time : 2017/9/20 10:19
 * @description :
 * @file : BRCFDTagsListFragment.java
 */

public class BRCFDTagsUpdateListFragment extends BaseFragment {

    private RecyclerView mRecyclerView;
    private BRCFDTagsUpdateAdapter mRvAdapter;

    private BRCFDTagsBean mDataList;
    private int page;
    private String tagBusinessUkid;
    private ArrayList<BRCustomerFileDetailBean.TagsBean> tagIds;

    public interface OnButtonClickedListener {
        void onButtonClicked();
    }

    private OnButtonClickedListener mButtonClickedListener;

    public void setOnButtonClickedListener(OnButtonClickedListener onButtonClickedListener) {
        mButtonClickedListener = onButtonClickedListener;
    }

    public static BRCFDTagsUpdateListFragment newInstance(int page, BRCFDTagsBean bean, String tagBusinessUkid, ArrayList<BRCustomerFileDetailBean.TagsBean> tagIds) {
        BRCFDTagsUpdateListFragment fragment = new BRCFDTagsUpdateListFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("tags", bean);
        bundle.putInt("page", page);
        bundle.putString("tagBusinessUkid", tagBusinessUkid);
        bundle.putParcelableArrayList("tagIds", tagIds);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mDataList = bundle.getParcelable("tags");
            page = bundle.getInt("page");
            tagBusinessUkid = bundle.getString("tagBusinessUkid");
            tagIds = bundle.getParcelableArrayList("tagIds");
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list_distribute_authority, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        if (page == 1)
            initData();
        else
            searchCmTagByBusinessUkid(page);

    }

    private void initData() {
        mRecyclerView = $(R.id.rv);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mRvAdapter = new BRCFDTagsUpdateAdapter(mDataList, mRecyclerView.getHeight(),tagIds);
                mRvAdapter.setOnItemClickListener(new BRCFDTagsUpdateAdapter.OnItemClickListener() {
                    @Override
                    public void onButtonClicked(int position) {
                        BRCFDTagsBean.ListBean listBean = mDataList.getList().get(position);

                        if (mButtonClickedListener != null) {
                            mButtonClickedListener.onButtonClicked();
                        }
                        BRAddTagsFragment brAddTagsFragment = new BRAddTagsFragment();
                        Bundle bundle = new Bundle();
                        bundle.putInt("from",BRAddTagsFragment.FROM_UPDATE);
                        bundle.putString("tagUkid",listBean.getTagUkid() + "");
                        bundle.putString("tagName",listBean.getTagName() + "");
                        bundle.putString("tagType",listBean.getTagType() + "");
                        bundle.putString("tagBusinessUkid", tagBusinessUkid);
                        brAddTagsFragment.setArguments(bundle);
                        pushFragment(brAddTagsFragment);
                        mRvAdapter.notifyDataSetChanged();
                    }
                });
                mRecyclerView.setAdapter(mRvAdapter);
                mRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }

    private void searchCmTagByBusinessUkid(int page){
        Map<String, Object> map = new HashMap<>();
        map.put("tagType", "BU_REL_CUS");
        map.put("tagBusinessUkid", tagBusinessUkid);
        map.put("page", page);
        map.put("size", 27);
        NoHttpUtils.httpPost(UserCenterConstant.SEARCH_CMTAG_BY_BUSINESS_UKID, map, onResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener onResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case 0:
                    if (TextUtils.equals("0", commonClass.getCode())) {
                        mDataList = JSON.parseObject(commonClass.getData().toString(), BRCFDTagsBean.class);
                        initData();

                    } else {
                        ToastUtils.showToast(commonClass.getMsg());
                    }
                    break;
            }
        }

        @Override
        public void onFailed(String s, int i) {

        }

        @Override
        public void onFinish(int i) {
            dismissProgressDialog();
        }
    };

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }
}
